/*
 * @Author: White
 * @description: tools
 * @LastEditTime: 2021-09-01 10:40:22
 */

// 路由懒加载的工具函数
export const lazyLoad = (viewPath) => {
  return () => import(`@/views/${viewPath}.vue`);
}

// 时间转换
export const timeFormat = (value) => {
  const dt = new Date(value)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')
  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

// 将数字转换成万 并且保留两位小数
export const keepTwoDecimalFull = (num) => {
    if (num > 10000) {
        let result = num / 10000;
        result = Math.floor(result * 100) / 100;
        var s_x = result.toString(); //将数字转换为字符串
        var pos_decimal = s_x.indexOf('.'); //小数点的索引值
        // 当整数时，pos_decimal=-1 自动补0
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        // 当数字的长度< 小数点索引+2时，补0
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        s_x += '万';
    } else {
        s_x = num;
    }
    return s_x
}

/**
 * 把秒数转换为分钟数
 * @param duration 秒数(0-3600)
 * @returns
 */
 export const second = (duration) => {
  let min = parseInt(duration / 1000 / 60);
  let sec = parseInt((duration / 1000) % 60);
  if (min < 10) {
    min = "0" + min;
  }
  if (sec < 10) {
    sec = "0" + sec;
  }
  return `${min}:${sec}`
}
